package com.charging.business.vo.obj;

import com.alibaba.fastjson2.annotation.JSONField;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JerryLu
 * @date 2024/9/26 17:24
 * @description 出口兜底计费信息
 */
//@Data
public class MiniFeeInfo {
    /**
     * 最小费额应收金额
     * 必填：是
     */
    @JSONField(ordinal = 0)
    public Integer calMinFeeFee = 0;

    /**
     * 最小费额95折实收金额
     * 必填：是
     */
    @JSONField(ordinal = 1)
    public Integer calMinFeeFee95 = 0;

    /**
     * 最小费额里程
     * 必填：否
     */
    @JSONField(ordinal = 2)
    public Integer calMinFeeMileage = 0;

    /**
     * 全网最小费额费率版本号
     * 必填：是
     */
    @JSONField(ordinal = 3)
    public String spcRateVersion = "";

    /**
     * 分省金额列表
     * 必填：是
     */
    @JSONField(ordinal = 4)
    public List<SplitProvinceMini> splitProvince = new ArrayList<>();

    /**
     * 自定义字段
     * 兜底应收总金额
     * 必填：是
     * MTC - 为优惠前的calMinFeeFee金额
     * ETC - 为优惠前的calMinFeeFee95金额
     */
    @JSONField(ordinal = 5, serialize = false, deserialize = false)
    public Integer zMinPayFee = 0;

    /**
     * 自定义字段
     * 兜底实收总金额
     * 必填：是
     * MTC - 为优惠后的calMinFeeFee金额
     * ETC - 为优惠后的calMinFeeFee95金额
     */
    @JSONField(ordinal = 6, serialize = false, deserialize = false)
    public Integer zMinFee = 0;

    /**
     * 合并分省信息
     */
    @JSONField(serialize = false, deserialize = false)
    public void merge() {
        if (splitProvince.size() > 1) {// 2个及以上
            Map<Integer, SplitProvinceMini> provs = new LinkedHashMap<>();

            for (SplitProvinceMini src : splitProvince) {
                SplitProvinceMini mer = provs.get(src.provinceId);
                if (mer == null) {
                    provs.put(src.provinceId, src);
                } else {
                    mer.ziMinPayFee += src.ziMinPayFee; //todo
                    mer.ziMinFee += src.ziMinFee; //todo
                    mer.calMinFeeFee += src.calMinFeeFee;
                    mer.calMinFeeFee95 += src.calMinFeeFee95;
                    mer.calMinFeeMileage += src.calMinFeeMileage;
                    mer.tollIntervalsCount += src.tollIntervalsCount;

                    if (!mer.tollIntervalsGroup.isEmpty()) {
                        if (!src.tollIntervalsGroup.isEmpty()) {
                            mer.tollIntervalsGroup += "|" + src.tollIntervalsGroup;
                        }
                    } else {
                        if (!src.tollIntervalsGroup.isEmpty()) {
                            mer.tollIntervalsGroup = src.tollIntervalsGroup;
                        }
                    }

                    if (!mer.feeGroup.isEmpty()) {
                        if (!src.feeGroup.isEmpty()) {
                            mer.feeGroup += "|" + src.feeGroup;
                        }
                    } else {
                        if (!src.feeGroup.isEmpty()) {
                            mer.feeGroup = src.feeGroup;
                        }
                    }
                }
            }

            splitProvince.clear();
            for (Integer key : provs.keySet()) {
                splitProvince.add(provs.get(key));
            }
        }
    }

    /**
     * 多省判断
     *
     * @return true - 为多省
     * false - 不为多省
     */
    @JSONField(serialize = false, deserialize = false)
    public boolean chkMutiProv() {
        return splitProvince.size() > 1;
    }

    /**
     * 绿通、联合收割机免费处理
     * 分省信息保持不变
     */
    @JSONField(serialize = false, deserialize = false)
    public void green() {
        zMinFee = 0;
        calMinFeeFee = 0;
        calMinFeeFee95 = 0;
    }

    /**
     * 全免费,清除计费信息
     */
    @JSONField(serialize = false, deserialize = false)
    public void toAllFree() {
        zMinFee = 0;
        calMinFeeFee = 0;
        calMinFeeFee95 = 0;
        for (SplitProvinceMini prov : splitProvince) {
            prov.toFree();
        }
    }

    /**
     * 本省免费,清除本省计费信息
     */
    @JSONField(serialize = false, deserialize = false)
    public void toLocalFree() {
        zMinFee = 0;
        calMinFeeFee = 0;
        calMinFeeFee95 = 0;
        for (SplitProvinceMini prov : splitProvince) {
            if (prov.provinceId == 14) {// 本省免费
                prov.toFree();
            }
            zMinFee += prov.ziMinFee;
            calMinFeeFee += prov.calMinFeeFee;
            calMinFeeFee95 += prov.calMinFeeFee95;
        }
    }

    /**
     * 获取本省MTC免费后收费总金额
     * 用于进行阈值比较
     *
     * @return
     */
    @JSONField(serialize = false, deserialize = false)
    public int mtcSum100FeeAftLFree() {
        int res = 0;
        for (SplitProvinceMini prov : splitProvince) {
            if (prov.provinceId != 14) {// 外省
                res += prov.calMinFeeFee;
            }
        }
        return res;
    }

    /**
     * 获取本省累计应收
     *
     * @return
     */
    @JSONField(serialize = false, deserialize = false)
    public int payFeeProvSumLocal() {
        int res = 0;
        for (SplitProvinceMini prov : splitProvince) {
            if (prov.provinceId == 14) {// 本省
                res += prov.ziMinPayFee;
            }
        }

        return res;
    }

    /**
     * 获取外省累计应收,山西
     *
     * @return
     */
    @JSONField(serialize = false, deserialize = false)
    public int payFeeProvSumOther() {
        int res = 0;
        for (SplitProvinceMini prov : splitProvince) {
            if (prov.provinceId != 14) {// 外省
                res += prov.ziMinPayFee;
            }
        }

        return res;
    }

    /**
     * 获取本省累计实收,陕西
     *
     * @return
     */
    @JSONField(serialize = false, deserialize = false)
    public int feeProvSumLocal() {
        int res = 0;
        for (SplitProvinceMini prov : splitProvince) {
            if (prov.provinceId == 61) {// 本省
                res += prov.ziMinFee;
            }
        }

        return res;
    }

    /**
     * 获取本省累计里程
     *
     * @return
     */
    @JSONField(serialize = false, deserialize = false)
    public int feeMileageProvSumLocal() {
        int res = 0;
        for (SplitProvinceMini prov : splitProvince) {
            if (prov.provinceId == 14) {// 本省
                res += prov.calMinFeeMileage;
            }
        }

        return res;
    }
}
